home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cp / cvt.c < prev    next >
C/C++ Source or Header  |  1994-07-05  |  61KB  |  2,045 lines

  1. /* Language-level data type conversion for GNU C++.
  2.    Copyright (C) 1987, 1988, 1992, 1993 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the functions for converting C expressions
  23.    to different data types.  The only entry point is `convert'.
  24.    Every language front end must have a `convert' function
  25.    but what kind of conversions it does will depend on the language.  */
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "flags.h"
  30. #include "cp-tree.h"
  31. #include "class.h"
  32. #include "convert.h"
  33.  
  34. #undef NULL
  35. #define NULL (char *)0
  36.  
  37. /* Change of width--truncation and extension of integers or reals--
  38.    is represented with NOP_EXPR.  Proper functioning of many things
  39.    assumes that no other conversions can be NOP_EXPRs.
  40.  
  41.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  42.    Converting integer to real uses FLOAT_EXPR
  43.    and real to integer uses FIX_TRUNC_EXPR.
  44.  
  45.    Here is a list of all the functions that assume that widening and
  46.    narrowing is always done with a NOP_EXPR:
  47.      In convert.c, convert_to_integer.
  48.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  49.         and truthvalue_conversion.
  50.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  51.      In fold-const.c: fold.
  52.      In tree.c: get_narrower and get_unwidened.
  53.  
  54.    C++: in multiple-inheritance, converting between pointers may involve
  55.    adjusting them by a delta stored within the class definition.  */
  56.  
  57. /* Subroutines of `convert'.  */
  58.  
  59. /* Build a thunk.  What it is, is an entry point that when called will
  60.    adjust the this pointer (the first argument) by offset, and then
  61.    goto the real address of the function given by REAL_ADDR that we
  62.    would like called.  What we return is the address of the thunk.  */
  63. static tree
  64. build_thunk (offset, real_addr)
  65.      tree offset, real_addr;
  66. {
  67.   if (TREE_CODE (real_addr) != ADDR_EXPR
  68.       || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
  69.     {
  70.       sorry ("MI pointer to member conversion too complex");
  71.       return error_mark_node;
  72.     }
  73.   sorry ("MI pointer to member conversion too complex");
  74.   return error_mark_node;
  75. }
  76.  
  77. /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
  78.    another `pointer to method'.  This may involved the creation of
  79.    a thunk to handle the this offset calculation.  */
  80. static tree
  81. convert_fn_ptr (type, expr)
  82.      tree type, expr;
  83. {
  84.   tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (expr))),
  85.               TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
  86.               1);
  87.   if (binfo == error_mark_node)
  88.     {
  89.       error ("  in pointer to member conversion");
  90.       return error_mark_node;
  91.     }
  92.   if (binfo == NULL_TREE)
  93.     {
  94.       /* ARM 4.8 restriction. */
  95.       error ("invalid pointer to member conversion");
  96.       return error_mark_node;
  97.     }
  98.   if (BINFO_OFFSET_ZEROP (binfo))
  99.     return build1 (NOP_EXPR, type, expr);
  100.   return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
  101. }
  102.  
  103. /* if converting pointer to pointer
  104.      if dealing with classes, check for derived->base or vice versa
  105.      else if dealing with method pointers, delegate
  106.      else convert blindly
  107.    else if converting class, pass off to build_type_conversion
  108.    else try C-style pointer conversion  */
  109. static tree
  110. cp_convert_to_pointer (type, expr)
  111.      tree type, expr;
  112. {
  113.   register tree intype = TREE_TYPE (expr);
  114.   register enum tree_code form = TREE_CODE (intype);
  115.   
  116.   if (form == POINTER_TYPE)
  117.     {
  118.       intype = TYPE_MAIN_VARIANT (intype);
  119.  
  120.       if (TYPE_MAIN_VARIANT (type) != intype
  121.       && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  122.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
  123.     {
  124.       enum tree_code code = PLUS_EXPR;
  125.       tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
  126.       if (binfo == error_mark_node)
  127.         return error_mark_node;
  128.       if (binfo == NULL_TREE)
  129.         {
  130.           binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
  131.           if (binfo == error_mark_node)
  132.         return error_mark_node;
  133.           code = MINUS_EXPR;
  134.         }
  135.       if (binfo)
  136.         {
  137.           if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
  138.           || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
  139.           || ! BINFO_OFFSET_ZEROP (binfo))
  140.         {
  141.           /* Need to get the path we took.  */
  142.           tree path;
  143.  
  144.           if (code == PLUS_EXPR)
  145.             get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
  146.           else
  147.             get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
  148.           return build_vbase_path (code, type, expr, path, 0);
  149.         }
  150.         }
  151.     }
  152.       if (TYPE_MAIN_VARIANT (type) != intype
  153.       && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
  154.       && TREE_CODE (type) == POINTER_TYPE
  155.       && TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE)
  156.     return convert_fn_ptr (type, expr);
  157.  
  158.       return build1 (NOP_EXPR, type, expr);
  159.     }
  160.  
  161.   my_friendly_assert (form != OFFSET_TYPE, 186);
  162.  
  163.   if (TYPE_LANG_SPECIFIC (intype)
  164.       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
  165.     return convert_to_pointer (type, build_optr_ref (expr));
  166.  
  167.   if (IS_AGGR_TYPE (intype))
  168.     {
  169.       tree rval;
  170.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  171.       if (rval)
  172.     {
  173.       if (rval == error_mark_node)
  174.         cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
  175.               expr, intype, type);
  176.       return rval;
  177.     }
  178.     }
  179.  
  180.   if (integer_zerop (expr))
  181.     {
  182.       if (type == TREE_TYPE (null_pointer_node))
  183.     return null_pointer_node;
  184.       expr = build_int_2 (0, 0);
  185.       TREE_TYPE (expr) = type;
  186.       return expr;
  187.     }
  188.  
  189.   if (INTEGRAL_CODE_P (form))
  190.     {
  191.       if (type_precision (intype) == POINTER_SIZE)
  192.     return build1 (CONVERT_EXPR, type, expr);
  193.       expr = convert (type_for_size (POINTER_SIZE, 0), expr);
  194.       /* Modes may be different but sizes should be the same.  */
  195.       if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
  196.       != GET_MODE_SIZE (TYPE_MODE (type)))
  197.     /* There is supposed to be some integral type
  198.        that is the same width as a pointer.  */
  199.     abort ();
  200.       return convert_to_pointer (type, expr);
  201.     }
  202.  
  203.   cp_error ("cannot convert `%E' from type `%T' to type `%T'",
  204.         expr, intype, type);
  205.   return error_mark_node;
  206. }
  207.  
  208. /* Like convert, except permit conversions to take place which
  209.    are not normally allowed due to access restrictions
  210.    (such as conversion from sub-type to private super-type).  */
  211. static tree
  212. convert_to_pointer_force (type, expr)
  213.      tree type, expr;
  214. {
  215.   register tree intype = TREE_TYPE (expr);
  216.   register enum tree_code form = TREE_CODE (intype);
  217.   
  218.   if (integer_zerop (expr))
  219.     {
  220.       if (type == TREE_TYPE (null_pointer_node))
  221.     return null_pointer_node;
  222.       expr = build_int_2 (0, 0);
  223.       TREE_TYPE (expr) = type;
  224.       return expr;
  225.     }
  226.  
  227.   /* Convert signature pointer/reference to `void *' first.  */
  228.   if (form == RECORD_TYPE
  229.       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
  230.     {
  231.       expr = build_optr_ref (expr);
  232.       intype = TREE_TYPE (expr);
  233.       form = TREE_CODE (intype);
  234.     }
  235.  
  236.   if (form == POINTER_TYPE)
  237.     {
  238.       intype = TYPE_MAIN_VARIANT (intype);
  239.  
  240.       if (TYPE_MAIN_VARIANT (type) != intype
  241.       && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  242.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
  243.     {
  244.       enum tree_code code = PLUS_EXPR;
  245.       tree path;
  246.       int distance = get_base_distance (TREE_TYPE (type),
  247.                         TREE_TYPE (intype), 0, &path);
  248.       if (distance == -2)
  249.         {
  250.         ambig:
  251.           cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
  252.                     TYPE_NAME_STRING (TREE_TYPE (intype)));
  253.           return error_mark_node;
  254.         }
  255.       if (distance == -1)
  256.         {
  257.           distance = get_base_distance (TREE_TYPE (intype),
  258.                         TREE_TYPE (type), 0, &path);
  259.           if (distance == -2)
  260.         goto ambig;
  261.           if (distance < 0)
  262.         /* Doesn't need any special help from us.  */
  263.         return build1 (NOP_EXPR, type, expr);
  264.  
  265.           code = MINUS_EXPR;
  266.         }
  267.       return build_vbase_path (code, type, expr, path, 0);
  268.     }
  269.       return build1 (NOP_EXPR, type, expr);
  270.     }
  271.  
  272.   return cp_convert_to_pointer (type, expr);
  273. }
  274.  
  275. /* We are passing something to a function which requires a reference.
  276.    The type we are interested in is in TYPE. The initial
  277.    value we have to begin with is in ARG.
  278.  
  279.    FLAGS controls how we manage access checking.
  280.    CHECKCONST controls if we report error messages on const subversion.  */
  281. static tree
  282. build_up_reference (type, arg, flags, checkconst)
  283.      tree type, arg;
  284.      int flags, checkconst;
  285. {
  286.   tree rval, targ;
  287.   int literal_flag = 0;
  288.   tree argtype = TREE_TYPE (arg);
  289.   tree target_type = TREE_TYPE (type);
  290.   tree binfo = NULL_TREE;
  291.  
  292.   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
  293.   if ((flags & LOOKUP_PROTECT)
  294.       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
  295.       && IS_AGGR_TYPE (argtype)
  296.       && IS_AGGR_TYPE (target_type))
  297.     {
  298.       binfo = get_binfo (target_type, argtype, 1);
  299.       if (binfo == error_mark_node)
  300.     return error_mark_node;
  301.       if (binfo == NULL_TREE)
  302.     return error_not_base_type (target_type, argtype);
  303.     }
  304.  
  305.   /* Pass along const and volatile down into the type. */
  306.   if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
  307.     target_type = c_build_type_variant (target_type, TYPE_READONLY (type),
  308.                     TYPE_VOLATILE (type));
  309.   targ = arg;
  310.   if (TREE_CODE (targ) == SAVE_EXPR)
  311.     targ = TREE_OPERAND (targ, 0);
  312.  
  313.   switch (TREE_CODE (targ))
  314.     {
  315.     case INDIRECT_REF:
  316.       /* This is a call to a constructor which did not know what it was
  317.      initializing until now: it needs to initialize a temporary.  */
  318.       if (TREE_HAS_CONSTRUCTOR (targ))
  319.     {
  320.       tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
  321.       TREE_HAS_CONSTRUCTOR (targ) = 0;
  322.       return build_up_reference (type, temp, flags, 1);
  323.     }
  324.       /* Let &* cancel out to simplify resulting code.
  325.          Also, throw away intervening NOP_EXPRs.  */
  326.       arg = TREE_OPERAND (targ, 0);
  327.       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
  328.       || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
  329.     arg = TREE_OPERAND (arg, 0);
  330.  
  331.       /* in doing a &*, we have to get rid of the const'ness on the pointer
  332.      value.  Haven't thought about volatile here.  Pointers come to mind
  333.      here.  */
  334.       if (TREE_READONLY (arg))
  335.     {
  336.       arg = copy_node (arg);
  337.       TREE_READONLY (arg) = 0;
  338.     }
  339.  
  340.       rval = build1 (CONVERT_EXPR, type, arg);
  341.       TREE_REFERENCE_EXPR (rval) = 1;
  342.  
  343.       /* propagate the const flag on something like:
  344.  
  345.      class Base {
  346.      public:
  347.        int foo;
  348.      };
  349.  
  350.       class Derived : public Base {
  351.       public:
  352.     int bar;
  353.       };
  354.  
  355.       void func(Base&);
  356.  
  357.       void func2(const Derived& d) {
  358.     func(d);
  359.       }
  360.  
  361.         on the d parameter.  The below could have been avoided, if the flags
  362.         were down in the tree, not sure why they are not.  (mrs) */
  363.       /* The below code may have to be propagated to other parts of this
  364.      switch.  */
  365.       if (TREE_READONLY (targ) && !TREE_READONLY (arg)
  366.       && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
  367.       && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
  368.       && (TYPE_READONLY (target_type) && checkconst))
  369.     {
  370.       arg = copy_node (arg);
  371.       TREE_READONLY (arg) = TREE_READONLY (targ);
  372.     }
  373.       literal_flag = TREE_CONSTANT (arg);
  374.  
  375.       goto done;
  376.  
  377.       /* Get this out of a register if we happened to be in one by accident.
  378.      Also, build up references to non-lvalues it we must.  */
  379.       /* For &x[y], return (&) x+y */
  380.     case ARRAY_REF:
  381.       if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
  382.     return error_mark_node;
  383.       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
  384.                   TREE_OPERAND (targ, 1), 1);
  385.       TREE_TYPE (rval) = type;
  386.       if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
  387.       && staticp (TREE_OPERAND (targ, 0)))
  388.     TREE_CONSTANT (rval) = 1;
  389.       goto done;
  390.  
  391.     case SCOPE_REF:
  392.       /* Could be a reference to a static member.  */
  393.       {
  394.     tree field = TREE_OPERAND (targ, 1);
  395.     if (TREE_STATIC (field))
  396.       {
  397.         rval = build1 (ADDR_EXPR, type, field);
  398.         literal_flag = 1;
  399.         goto done;
  400.       }
  401.       }
  402.  
  403.       /* We should have farmed out member pointers above.  */
  404.       my_friendly_abort (188);
  405.  
  406.     case COMPONENT_REF:
  407.       rval = build_component_addr (targ, build_pointer_type (argtype),
  408.                    "attempt to make a reference to bit-field structure member `%s'");
  409.       TREE_TYPE (rval) = type;
  410.       literal_flag = staticp (TREE_OPERAND (targ, 0));
  411.  
  412.       goto done;
  413.  
  414.       /* Anything not already handled and not a true memory reference
  415.      needs to have a reference built up.  Do so silently for
  416.      things like integers and return values from function,
  417.      but complain if we need a reference to something declared
  418.      as `register'.  */
  419.  
  420.     case RESULT_DECL:
  421.       if (staticp (targ))
  422.     literal_flag = 1;
  423.       TREE_ADDRESSABLE (targ) = 1;
  424.       put_var_into_stack (targ);
  425.       break;
  426.  
  427.     case PARM_DECL:
  428.       if (targ == current_class_decl)
  429.     {
  430.       error ("address of `this' not available");
  431. #if 0
  432.       /* This code makes the following core dump the compiler on a sun4,
  433.          if the code below is used.
  434.  
  435.          class e_decl;
  436.          class a_decl;
  437.          typedef a_decl* a_ref;
  438.  
  439.          class a_s {
  440.          public:
  441.            a_s();
  442.            void* append(a_ref& item);
  443.          };
  444.          class a_decl {
  445.          public:
  446.            a_decl (e_decl *parent);
  447.            a_s  generic_s;
  448.            a_s  decls;
  449.            e_decl* parent;
  450.          };
  451.  
  452.          class e_decl {
  453.          public:
  454.            e_decl();
  455.            a_s implementations;
  456.          };
  457.  
  458.          void foobar(void *);
  459.  
  460.          a_decl::a_decl(e_decl *parent) {
  461.            parent->implementations.append(this);
  462.          }
  463.        */
  464.  
  465.       TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
  466.       put_var_into_stack (targ);
  467.       break;
  468. #else
  469.       return error_mark_node;
  470. #endif
  471.     }
  472.       /* Fall through.  */
  473.     case VAR_DECL:
  474.     case CONST_DECL:
  475.       if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ))
  476.     warning ("address needed to build reference for `%s', which is declared `register'",
  477.          IDENTIFIER_POINTER (DECL_NAME (targ)));
  478.       else if (staticp (targ))
  479.     literal_flag = 1;
  480.  
  481.       TREE_ADDRESSABLE (targ) = 1;
  482.       put_var_into_stack (targ);
  483.       break;
  484.  
  485.     case COMPOUND_EXPR:
  486.       {
  487.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
  488.                           LOOKUP_PROTECT, checkconst);
  489.     rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
  490.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
  491.     return rval;
  492.       }
  493.  
  494.     case MODIFY_EXPR:
  495.     case INIT_EXPR:
  496.       {
  497.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
  498.                           LOOKUP_PROTECT, checkconst);
  499.     rval = build (COMPOUND_EXPR, type, arg, real_reference);
  500.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
  501.     return rval;
  502.       }
  503.  
  504.     case COND_EXPR:
  505.       return build (COND_EXPR, type,
  506.             TREE_OPERAND (targ, 0),
  507.             build_up_reference (type, TREE_OPERAND (targ, 1),
  508.                     LOOKUP_PROTECT, checkconst),
  509.             build_up_reference (type, TREE_OPERAND (targ, 2),
  510.                     LOOKUP_PROTECT, checkconst));
  511.  
  512.     case WITH_CLEANUP_EXPR:
  513.       return build (WITH_CLEANUP_EXPR, type,
  514.             build_up_reference (type, TREE_OPERAND (targ, 0),
  515.                     LOOKUP_PROTECT, checkconst),
  516.             0, TREE_OPERAND (targ, 2));
  517.  
  518.     case BIND_EXPR:
  519.       arg = TREE_OPERAND (targ, 1);
  520.       if (arg == NULL_TREE)
  521.     {
  522.       compiler_error ("({ ... }) expression not expanded when needed for reference");
  523.       return error_mark_node;
  524.     }
  525.       rval = build1 (ADDR_EXPR, type, arg);
  526.       TREE_REFERENCE_EXPR (rval) = 1;
  527.       return rval;
  528.  
  529.     default:
  530.       break;
  531.     }
  532.  
  533.   if (TREE_ADDRESSABLE (targ) == 0)
  534.     {
  535.       tree temp;
  536.  
  537.       if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
  538.     {
  539.       temp = build_cplus_new (argtype, targ, 1);
  540.       if (TREE_CODE (temp) == WITH_CLEANUP_EXPR)
  541.         rval = build (WITH_CLEANUP_EXPR, type,
  542.               build1 (ADDR_EXPR, type, TREE_OPERAND (temp, 0)),
  543.               0, TREE_OPERAND (temp, 2));
  544.       else
  545.         rval = build1 (ADDR_EXPR, type, temp);
  546.       goto done;
  547.     }
  548.       else
  549.     {
  550.       temp = get_temp_name (argtype, 0);
  551.       if (global_bindings_p ())
  552.         {
  553.           /* Give this new temp some rtl and initialize it.  */
  554.           DECL_INITIAL (temp) = targ;
  555.           TREE_STATIC (temp) = 1;
  556.           finish_decl (temp, targ, NULL_TREE, 0);
  557.           /* Do this after declaring it static.  */
  558.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  559.           TREE_TYPE (rval) = type;
  560.           literal_flag = TREE_CONSTANT (rval);
  561.           goto done;
  562.         }
  563.       else
  564.         {
  565.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  566.           if (binfo && !BINFO_OFFSET_ZEROP (binfo))
  567.         rval = convert_pointer_to (target_type, rval);
  568.           else
  569.         TREE_TYPE (rval) = type;
  570.  
  571.           temp = build (MODIFY_EXPR, argtype, temp, arg);
  572.           TREE_SIDE_EFFECTS (temp) = 1;
  573.           return build (COMPOUND_EXPR, type, temp, rval);
  574.         }
  575.     }
  576.     }
  577.   else
  578.     rval = build1 (ADDR_EXPR, type, arg);
  579.  
  580.  done:
  581.   if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
  582.       || TYPE_USES_COMPLEX_INHERITANCE (target_type))
  583.     {
  584.       TREE_TYPE (rval) = build_pointer_type (argtype);
  585.       if (flags & LOOKUP_PROTECT)
  586.     rval = convert_pointer_to (target_type, rval);
  587.       else
  588.     rval
  589.       = convert_to_pointer_force (build_pointer_type (target_type), rval);
  590.       TREE_TYPE (rval) = type;
  591.     }
  592.   TREE_CONSTANT (rval) = literal_flag;
  593.   return rval;
  594. }
  595.  
  596. /* For C++: Only need to do one-level references, but cannot
  597.    get tripped up on signed/unsigned differences.
  598.  
  599.    DECL is either NULL_TREE or the _DECL node for a reference that is being
  600.    initialized.  It can be error_mark_node if we don't know the _DECL but
  601.    we know it's an initialization.  */
  602.  
  603. tree cp_convert PROTO((tree, tree, int, int));
  604.  
  605. tree
  606. convert_to_reference (reftype, expr, convtype, flags, decl)
  607.      tree reftype, expr;
  608.      int convtype, flags;
  609.      tree decl;
  610. {
  611.   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
  612.   register tree intype = TREE_TYPE (expr);
  613.   register enum tree_code form = TREE_CODE (intype);
  614.   tree rval = NULL_TREE;
  615.  
  616.   if (form == REFERENCE_TYPE)
  617.     intype = TREE_TYPE (intype);
  618.   intype = TYPE_MAIN_VARIANT (intype);
  619.  
  620.   if (((convtype & CONV_STATIC) && comptypes (type, intype, -1))
  621.       || ((convtype & CONV_IMPLICIT) && comptypes (type, intype, 0)))
  622.     {
  623.       if (flags & LOOKUP_COMPLAIN)
  624.     {
  625.       tree ttl = TREE_TYPE (reftype);
  626.       tree ttr;
  627.       
  628.       if (form == REFERENCE_TYPE)
  629.         ttr = TREE_TYPE (TREE_TYPE (expr));
  630.       else
  631.         {
  632.           int r = TREE_READONLY (expr);
  633.           int v = TREE_THIS_VOLATILE (expr);
  634.           ttr = c_build_type_variant (TREE_TYPE (expr), r, v);
  635.         }
  636.  
  637.       if (! lvalue_p (expr) &&
  638.           (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
  639.         {
  640.           if (decl)
  641.         /* Ensure semantics of [dcl.init.ref] */
  642.         cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
  643.                 reftype, intype);
  644.           else
  645.         cp_pedwarn ("conversion to `%T' from rvalue `%T'",
  646.                 reftype, intype);
  647.         }
  648.       else if (! (convtype & CONV_CONST))
  649.         {
  650.           if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
  651.         cp_pedwarn ("conversion from `%T' to `%T' discards const",
  652.                 ttr, reftype);
  653.           else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
  654.         cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
  655.                 ttr, reftype);
  656.         }
  657.     }
  658.  
  659.       if (form == REFERENCE_TYPE)
  660.     {
  661.       rval = copy_node (expr);
  662.       TREE_TYPE (rval) = build_pointer_type (TREE_TYPE (TREE_TYPE (expr)));
  663.       rval = cp_convert (build_pointer_type (TREE_TYPE (reftype)), rval,
  664.                  convtype, flags);
  665.       TREE_TYPE (rval) = reftype;
  666.       return rval;
  667.     }
  668.  
  669.       return build_up_reference (reftype, expr, flags,
  670.                  ! (convtype & CONV_CONST));
  671.     }
  672.  
  673.   if ((convtype & CONV_IMPLICIT)
  674.       && IS_AGGR_TYPE (intype)
  675.       && ! (flags & LOOKUP_NO_CONVERSION)
  676.       && (rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1)))
  677.     {
  678.       if (rval == error_mark_node)
  679.     cp_error ("conversion from `%T' to `%T' is ambiguous",
  680.           intype, reftype);
  681.       return rval;
  682.     }
  683.   else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
  684.     {
  685.       /* When casting an lvalue to a reference type, just convert into
  686.      a pointer to the new type and deference it.  This is allowed
  687.      by San Diego WP section 5.2.9 paragraph 12, though perhaps it
  688.      should be done directly (jason).  (int &)ri ---> *(int*)&ri */
  689.  
  690.       /* B* bp; A& ar = (A&)bp; is legal, but it's probably not what they
  691.          meant.  */
  692.       if (form == POINTER_TYPE
  693.       && (comptypes (TREE_TYPE (intype), type, -1)))
  694.     cp_warning ("casting `%T' to `%T' does not dereference pointer",
  695.             intype, reftype);
  696.       
  697.       rval = build_unary_op (ADDR_EXPR, expr, 0);
  698.       if (rval != error_mark_node)
  699.     rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval);
  700.       if (rval != error_mark_node)
  701.     rval = build1 (NOP_EXPR, reftype, rval);
  702.     }
  703.   else if (decl)
  704.     {
  705.       tree rval_as_conversion = NULL_TREE;
  706.       tree rval_as_ctor = NULL_TREE;
  707.       
  708.       if (IS_AGGR_TYPE (intype)
  709.       && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1)))
  710.     {
  711.       if (rval == error_mark_node)
  712.         return rval;
  713.  
  714.       rval_as_conversion = build_up_reference (reftype, rval, flags, 1);
  715.     }
  716.       
  717.       /* Definitely need to go through a constructor here.  */
  718.       if (TYPE_HAS_CONSTRUCTOR (type)
  719.       && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
  720.       && (rval = build_method_call
  721.           (NULL_TREE, constructor_name_full (type),
  722.            build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
  723.            LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY)))
  724.     {
  725.       tree init;
  726.  
  727.       if (global_bindings_p ())
  728.         {
  729.           extern tree static_aggregates;
  730.           tree t = get_temp_name (type, global_bindings_p ());
  731.           init = build_method_call (t, constructor_name_full (type),
  732.                     build_tree_list (NULL_TREE, expr),
  733.                     TYPE_BINFO (type),
  734.                     LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
  735.  
  736.           if (init == error_mark_node)
  737.         return error_mark_node;
  738.  
  739.           make_decl_rtl (t, NULL_PTR, 1);
  740.           static_aggregates = perm_tree_cons (expr, t, static_aggregates);
  741.           rval = build_unary_op (ADDR_EXPR, t, 0);
  742.         }
  743.       else
  744.         {
  745.           init = build_method_call (NULL_TREE, constructor_name_full (type),
  746.                     build_tree_list (NULL_TREE, expr),
  747.                     TYPE_BINFO (type),
  748.                     LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
  749.  
  750.           if (init == error_mark_node)
  751.         return error_mark_node;
  752.  
  753.           rval = build_cplus_new (type, init, 1);
  754.           rval = build_up_reference (reftype, rval, flags, 1);
  755.         }
  756.       rval_as_ctor = rval;
  757.     }
  758.  
  759.       if (rval_as_ctor && rval_as_conversion)
  760.     {
  761.       cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
  762.             intype, reftype);
  763.       return error_mark_node;
  764.     }
  765.       else if (rval_as_ctor)
  766.     rval = rval_as_ctor;
  767.       else if (rval_as_conversion)
  768.     rval = rval_as_conversion;
  769.       else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
  770.     {
  771.       rval = convert (type, expr);
  772.       if (rval == error_mark_node)
  773.         return error_mark_node;
  774.       
  775.       rval = build_up_reference (reftype, rval, flags, 1);
  776.     }
  777.  
  778.       if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
  779.     cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
  780.             reftype, intype);
  781.     }
  782.  
  783.   if (rval)
  784.     {
  785.       /* If we found a way to convert earlier, then use it. */
  786.       return rval;
  787.     }
  788.  
  789.   my_friendly_assert (form != OFFSET_TYPE, 189);
  790.  
  791.   if (flags & LOOKUP_SPECULATIVELY)
  792.     return NULL_TREE;
  793.  
  794.   else if (flags & LOOKUP_COMPLAIN)
  795.     cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
  796.  
  797.   return error_mark_node;
  798. }
  799.  
  800. /* We are using a reference VAL for its value. Bash that reference all the
  801.    way down to its lowest form. */
  802. tree
  803. convert_from_reference (val)
  804.      tree val;
  805. {
  806.   tree type = TREE_TYPE (val);
  807.  
  808.   if (TREE_CODE (type) == OFFSET_TYPE)
  809.     type = TREE_TYPE (type);
  810.  if (TREE_CODE (type) == REFERENCE_TYPE)
  811.     {
  812.       tree target_type = TREE_TYPE (type);
  813.       tree nval;
  814.  
  815.       /* This can happen if we cast to a reference type.  */
  816.       if (TREE_CODE (val) == ADDR_EXPR)
  817.     {
  818.       nval = build1 (NOP_EXPR, build_pointer_type (target_type), val);
  819.       nval = build_indirect_ref (nval, NULL_PTR);
  820.       /* The below was missing, are other important flags missing too? */
  821.       TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val);
  822.       return nval;
  823.     }
  824.  
  825.       nval = build1 (INDIRECT_REF, target_type, val);
  826.  
  827.       TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type);
  828.       TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type);
  829.       TREE_READONLY (nval) = TYPE_READONLY (target_type);
  830.       /* The below was missing, are other important flags missing too? */
  831.       TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val);
  832.       return nval;
  833.     }
  834.   return val;
  835. }
  836.  
  837. /* See if there is a constructor of type TYPE which will convert
  838.    EXPR.  The reference manual seems to suggest (8.5.6) that we need
  839.    not worry about finding constructors for base classes, then converting
  840.    to the derived class.
  841.  
  842.    MSGP is a pointer to a message that would be an appropriate error
  843.    string.  If MSGP is NULL, then we are not interested in reporting
  844.    errors.  */
  845. tree
  846. convert_to_aggr (type, expr, msgp, protect)
  847.      tree type, expr;
  848.      char **msgp;
  849.      int protect;
  850. {
  851.   tree basetype = type;
  852.   tree name = TYPE_IDENTIFIER (basetype);
  853.   tree function, fndecl, fntype, parmtypes, parmlist, result;
  854.   tree method_name;
  855.   enum access_type access;
  856.   int can_be_private, can_be_protected;
  857.  
  858.   if (! TYPE_HAS_CONSTRUCTOR (basetype))
  859.     {
  860.       if (msgp)
  861.     *msgp = "type `%s' does not have a constructor";
  862.       return error_mark_node;
  863.     }
  864.  
  865.   access = access_public;
  866.   can_be_private = 0;
  867.   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
  868.  
  869.   parmlist = build_tree_list (NULL_TREE, expr);
  870.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
  871.  
  872.   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
  873.     {
  874.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  875.       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
  876.     }
  877.  
  878.   /* The type of the first argument will be filled in inside the loop.  */
  879.   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
  880.   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
  881.  
  882.   method_name = build_decl_overload (name, parmtypes, 1);
  883.  
  884.   /* constructors are up front.  */
  885.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  886.   if (TYPE_HAS_DESTRUCTOR (basetype))
  887.     fndecl = DECL_CHAIN (fndecl);
  888.  
  889.   while (fndecl)
  890.     {
  891.       if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
  892.     {
  893.       function = fndecl;
  894.       if (protect)
  895.         {
  896.           if (TREE_PRIVATE (fndecl))
  897.         {
  898.           can_be_private =
  899.             (basetype == current_class_type
  900.              || is_friend (basetype, current_function_decl)
  901.              || purpose_member (basetype, DECL_ACCESS (fndecl)));
  902.           if (! can_be_private)
  903.             goto found;
  904.         }
  905.           else if (TREE_PROTECTED (fndecl))
  906.         {
  907.           if (! can_be_protected)
  908.             goto found;
  909.         }
  910.         }
  911.       goto found_and_ok;
  912.     }
  913.       fndecl = DECL_CHAIN (fndecl);
  914.     }
  915.  
  916.   /* No exact conversion was found.  See if an approximate
  917.      one will do.  */
  918.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  919.   if (TYPE_HAS_DESTRUCTOR (basetype))
  920.     fndecl = DECL_CHAIN (fndecl);
  921.  
  922.   {
  923.     int saw_private = 0;
  924.     int saw_protected = 0;
  925.     struct candidate *candidates =
  926.       (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
  927.     struct candidate *cp = candidates;
  928.  
  929.     while (fndecl)
  930.       {
  931.     function = fndecl;
  932.     cp->h_len = 2;
  933.     cp->harshness = (struct harshness_code *)
  934.       alloca (3 * sizeof (struct harshness_code));
  935.  
  936.     compute_conversion_costs (fndecl, parmlist, cp, 2);
  937.     if ((cp->h.code & EVIL_CODE) == 0)
  938.       {
  939.         cp->u.field = fndecl;
  940.         if (protect)
  941.           {
  942.         if (TREE_PRIVATE (fndecl))
  943.           access = access_private;
  944.         else if (TREE_PROTECTED (fndecl))
  945.           access = access_protected;
  946.         else
  947.           access = access_public;
  948.           }
  949.         else
  950.           access = access_public;
  951.  
  952.         if (access == access_private
  953.         ? (basetype == current_class_type
  954.            || is_friend (basetype, cp->function)
  955.            || purpose_member (basetype, DECL_ACCESS (fndecl)))
  956.         : access == access_protected
  957.         ? (can_be_protected
  958.            || purpose_member (basetype, DECL_ACCESS (fndecl)))
  959.         : 1)
  960.           {
  961.         if (cp->h.code <= TRIVIAL_CODE)
  962.           goto found_and_ok;
  963.         cp++;
  964.           }
  965.         else
  966.           {
  967.         if (access == access_private)
  968.           saw_private = 1;
  969.         else
  970.           saw_protected = 1;
  971.           }
  972.       }
  973.     fndecl = DECL_CHAIN (fndecl);
  974.       }
  975.     if (cp - candidates)
  976.       {
  977.     /* Rank from worst to best.  Then cp will point to best one.
  978.        Private fields have their bits flipped.  For unsigned
  979.        numbers, this should make them look very large.
  980.        If the best alternate has a (signed) negative value,
  981.        then all we ever saw were private members.  */
  982.     if (cp - candidates > 1)
  983.       qsort (candidates,    /* char *base */
  984.          cp - candidates, /* int nel */
  985.          sizeof (struct candidate), /* int width */
  986.          rank_for_overload); /* int (*compar)() */
  987.  
  988.     --cp;
  989.     if (cp->h.code & EVIL_CODE)
  990.       {
  991.         if (msgp)
  992.           *msgp = "ambiguous type conversion possible for `%s'";
  993.         return error_mark_node;
  994.       }
  995.  
  996.     function = cp->function;
  997.     fndecl = cp->u.field;
  998.     goto found_and_ok;
  999.       }
  1000.     else if (msgp)
  1001.       {
  1002.     if (saw_private)
  1003.       if (saw_protected)
  1004.         *msgp = "only private and protected conversions apply";
  1005.       else
  1006.         *msgp = "only private conversions apply";
  1007.     else if (saw_protected)
  1008.       *msgp = "only protected conversions apply";
  1009.     else
  1010.       *msgp = "no appropriate conversion to type `%s'";
  1011.       }
  1012.     return error_mark_node;
  1013.   }
  1014.   /* NOTREACHED */
  1015.  
  1016.  found:
  1017.   if (access == access_private)
  1018.     if (! can_be_private)
  1019.       {
  1020.     if (msgp)
  1021.       *msgp = TREE_PRIVATE (fndecl)
  1022.         ? "conversion to type `%s' is private"
  1023.         : "conversion to type `%s' is from private base class";
  1024.     return error_mark_node;
  1025.       }
  1026.   if (access == access_protected)
  1027.     if (! can_be_protected)
  1028.       {
  1029.     if (msgp)
  1030.       *msgp = TREE_PRIVATE (fndecl)
  1031.         ? "conversion to type `%s' is protected"
  1032.         : "conversion to type `%s' is from protected base class";
  1033.     return error_mark_node;
  1034.       }
  1035.   function = fndecl;
  1036.  found_and_ok:
  1037.  
  1038.   /* It will convert, but we don't do anything about it yet.  */
  1039.   if (msgp == 0)
  1040.     return NULL_TREE;
  1041.  
  1042.   fntype = TREE_TYPE (function);
  1043.   if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  1044.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  1045.   else
  1046.     function = default_conversion (function);
  1047.  
  1048.   result = build_nt (CALL_EXPR, function,
  1049.              convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
  1050.                     parmlist, NULL_TREE, LOOKUP_NORMAL),
  1051.              NULL_TREE);
  1052.   TREE_TYPE (result) = TREE_TYPE (fntype);
  1053.   TREE_SIDE_EFFECTS (result) = 1;
  1054.   TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  1055.   return result;
  1056. }
  1057.  
  1058. /* Call this when we know (for any reason) that expr is not, in fact,
  1059.    zero.  This routine is like convert_pointer_to, but it pays
  1060.    attention to which specific instance of what type we want to
  1061.    convert to.  This routine should eventually become
  1062.    convert_to_pointer after all references to convert_to_pointer
  1063.    are removed.  */
  1064. tree
  1065. convert_pointer_to_real (binfo, expr)
  1066.      tree binfo, expr;
  1067. {
  1068.   register tree intype = TREE_TYPE (expr);
  1069.   tree ptr_type;
  1070.   tree type, rval;
  1071.  
  1072.   if (TREE_CODE (binfo) == TREE_VEC)
  1073.     type = BINFO_TYPE (binfo);
  1074.   else if (IS_AGGR_TYPE (binfo))
  1075.     {
  1076.       type = binfo;
  1077.     }
  1078.   else
  1079.     {
  1080.       type = binfo;
  1081.       binfo = NULL_TREE;
  1082.     }
  1083.  
  1084.   ptr_type = build_pointer_type (type);
  1085.   if (ptr_type == TYPE_MAIN_VARIANT (intype))
  1086.     return expr;
  1087.  
  1088.   if (intype == error_mark_node)
  1089.     return error_mark_node;
  1090.  
  1091.   my_friendly_assert (!integer_zerop (expr), 191);
  1092.  
  1093.   if (TREE_CODE (type) == RECORD_TYPE
  1094.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
  1095.       && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
  1096.     {
  1097.       tree path;
  1098.       int distance
  1099.     = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
  1100.                  0, &path);
  1101.  
  1102.       /* This function shouldn't be called with unqualified arguments
  1103.      but if it is, give them an error message that they can read.  */
  1104.       if (distance < 0)
  1105.     {
  1106.       cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
  1107.             TREE_TYPE (intype), type);
  1108.  
  1109.       if (distance == -2)
  1110.         cp_error ("because `%T' is an ambiguous base class", type);
  1111.       return error_mark_node;
  1112.     }
  1113.  
  1114.       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
  1115.     }
  1116.   rval = build1 (NOP_EXPR, ptr_type,
  1117.          TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
  1118.   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
  1119.   return rval;
  1120. }
  1121.  
  1122. /* Call this when we know (for any reason) that expr is
  1123.    not, in fact, zero.  This routine gets a type out of the first
  1124.    argument and uses it to search for the type to convert to.  If there
  1125.    is more than one instance of that type in the expr, the conversion is
  1126.    ambiguous.  This routine should eventually go away, and all
  1127.    callers should use convert_to_pointer_real.  */
  1128. tree
  1129. convert_pointer_to (binfo, expr)
  1130.      tree binfo, expr;
  1131. {
  1132.   tree type;
  1133.  
  1134.   if (TREE_CODE (binfo) == TREE_VEC)
  1135.     type = BINFO_TYPE (binfo);
  1136.   else if (IS_AGGR_TYPE (binfo))
  1137.       type = binfo;
  1138.   else
  1139.       type = binfo;
  1140.   return convert_pointer_to_real (type, expr);
  1141. }
  1142.  
  1143. /* Same as above, but don't abort if we get an "ambiguous" baseclass.
  1144.    There's only one virtual baseclass we are looking for, and once
  1145.    we find one such virtual baseclass, we have found them all.  */
  1146.  
  1147. tree
  1148. convert_pointer_to_vbase (binfo, expr)
  1149.      tree binfo;
  1150.      tree expr;
  1151. {
  1152.   tree intype = TREE_TYPE (TREE_TYPE (expr));
  1153.   tree binfos = TYPE_BINFO_BASETYPES (intype);
  1154.   int i;
  1155.  
  1156.   for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1157.     {
  1158.       tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
  1159.       if (BINFO_TYPE (binfo) == basetype)
  1160.     return convert_pointer_to (binfo, expr);
  1161.       if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
  1162.     return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
  1163.     }
  1164.   my_friendly_abort (6);
  1165.   /* NOTREACHED */
  1166.   return NULL_TREE;
  1167. }
  1168.  
  1169. tree
  1170. cp_convert (type, expr, convtype, flags)
  1171.      tree type, expr;
  1172.      int convtype, flags;
  1173. {
  1174.   register tree e = expr;
  1175.   register enum tree_code code = TREE_CODE (type);
  1176.  
  1177.   if (type == TREE_TYPE (e)
  1178.       || TREE_CODE (e) == ERROR_MARK)
  1179.     return e;
  1180.   if (TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
  1181.     return error_mark_node;
  1182.  
  1183.   /* Trivial conversion: cv-qualifiers do not matter on rvalues.  */
  1184.   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
  1185.     return fold (build1 (NOP_EXPR, type, e));
  1186.   
  1187.   if (code == VOID_TYPE && (convtype & CONV_STATIC))
  1188.     return build1 (CONVERT_EXPR, type, e);
  1189.  
  1190. #if 0
  1191.   /* This is incorrect.  A truncation can't be stripped this way.
  1192.      Extensions will be stripped by the use of get_unwidened.  */
  1193.   if (TREE_CODE (e) == NOP_EXPR)
  1194.     return convert (type, TREE_OPERAND (e, 0));
  1195. #endif
  1196.  
  1197.   /* Just convert to the type of the member.  */
  1198.   if (code == OFFSET_TYPE)
  1199.     {
  1200.       type = TREE_TYPE (type);
  1201.       code = TREE_CODE (type);
  1202.     }
  1203.  
  1204.   if (code == REFERENCE_TYPE)
  1205.     return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
  1206.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1207.     e = convert_from_reference (e);
  1208.  
  1209.   if (TREE_READONLY_DECL_P (e))
  1210.     e = decl_constant_value (e);
  1211.  
  1212.   if (INTEGRAL_CODE_P (code))
  1213.     {
  1214.       tree intype = TREE_TYPE (e);
  1215.       enum tree_code form = TREE_CODE (intype);
  1216.       /* enum = enum, enum = int, enum = float are all errors. */
  1217.       if (flag_int_enum_equivalence == 0
  1218.       && TREE_CODE (type) == ENUMERAL_TYPE
  1219.       && ARITHMETIC_TYPE_P (intype))
  1220.     {
  1221.       cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
  1222.  
  1223.       if (flag_pedantic_errors)
  1224.         return error_mark_node;
  1225.     }
  1226.       if (form == OFFSET_TYPE)
  1227.     cp_error_at ("pointer-to-member expression object not composed with type `%D' object",
  1228.              TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)));
  1229.       else if (IS_AGGR_TYPE (intype))
  1230.     {
  1231.       tree rval;
  1232.       rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
  1233.       if (rval) return rval;
  1234.       if (code == BOOLEAN_TYPE)
  1235.         cp_error ("`%#T' used where a `bool' was expected", intype);
  1236.       else
  1237.         cp_error ("`%#T' used where an `int' was expected", intype);
  1238.       return error_mark_node;
  1239.     }
  1240.       if (code == BOOLEAN_TYPE)
  1241.     {
  1242.       tree newe = truthvalue_conversion (e);
  1243.       /* Avoid stupid (infinite) recursion from backend. */
  1244.       if (TREE_CODE (newe) != NOP_EXPR || e != TREE_OPERAND (newe, 0))
  1245.         e = newe;
  1246.       if (TREE_TYPE (e) == bool_type_node)
  1247.         return e;
  1248.       else if (TREE_CODE (e) == INTEGER_CST)
  1249.         {
  1250.           if (e == integer_zero_node)
  1251.         e = false_node;
  1252.           else
  1253.         e = true_node;
  1254.         }
  1255.       else
  1256.         return build1 (NOP_EXPR, bool_type_node, e);
  1257.     }
  1258.       return fold (convert_to_integer (type, e));
  1259.     }
  1260.   if (code == POINTER_TYPE)
  1261.     return fold (cp_convert_to_pointer (type, e));
  1262.   if (code == REAL_TYPE)
  1263.     {
  1264.       if (IS_AGGR_TYPE (TREE_TYPE (e)))
  1265.     {
  1266.       tree rval;
  1267.       rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
  1268.       if (rval)
  1269.         return rval;
  1270.       else
  1271.         cp_error ("`%#T' used where a floating point value was expected",
  1272.               TREE_TYPE (e));
  1273.     }
  1274.       return fold (convert_to_real (type, e));
  1275.     }
  1276.  
  1277.   /* New C++ semantics:  since assignment is now based on
  1278.      memberwise copying,  if the rhs type is derived from the
  1279.      lhs type, then we may still do a conversion.  */
  1280.   if (IS_AGGR_TYPE_CODE (code))
  1281.     {
  1282.       tree dtype = TREE_TYPE (e);
  1283.  
  1284.       if (TREE_CODE (dtype) == REFERENCE_TYPE)
  1285.     {
  1286.       e = convert_from_reference (e);
  1287.       dtype = TREE_TYPE (e);
  1288.     }
  1289.       dtype = TYPE_MAIN_VARIANT (dtype);
  1290.  
  1291.       /* Conversion of object pointers or signature pointers/references
  1292.      to signature pointers/references.  */
  1293.  
  1294.       if (TYPE_LANG_SPECIFIC (type)
  1295.       && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
  1296.     {
  1297.       tree constructor = build_signature_pointer_constructor (type, expr);
  1298.       tree sig_ty = SIGNATURE_TYPE (type);
  1299.       tree sig_ptr;
  1300.  
  1301.       if (constructor == error_mark_node)
  1302.         return error_mark_node;
  1303.  
  1304.       sig_ptr = get_temp_name (type, 1);
  1305.       DECL_INITIAL (sig_ptr) = constructor;
  1306.       CLEAR_SIGNATURE (sig_ty);
  1307.       finish_decl (sig_ptr, constructor, 0, 0);
  1308.       SET_SIGNATURE (sig_ty);
  1309.       TREE_READONLY (sig_ptr) = 1;
  1310.  
  1311.       return sig_ptr;
  1312.     }
  1313.  
  1314.       /* Conversion between aggregate types.  New C++ semantics allow
  1315.      objects of derived type to be cast to objects of base type.
  1316.      Old semantics only allowed this between pointers.
  1317.  
  1318.      There may be some ambiguity between using a constructor
  1319.      vs. using a type conversion operator when both apply.  */
  1320.  
  1321.       else if (IS_AGGR_TYPE (dtype))
  1322.     {
  1323.       tree binfo;
  1324.  
  1325.       tree conversion;
  1326.  
  1327.       if (! DERIVED_FROM_P (type, dtype) && TYPE_HAS_CONVERSION (dtype))
  1328.         conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
  1329.       else
  1330.         conversion = NULL_TREE;
  1331.  
  1332.       if (TYPE_HAS_CONSTRUCTOR (type))
  1333.         {
  1334.           tree rval = build_method_call (NULL_TREE, constructor_name_full (type),
  1335.                          build_tree_list (NULL_TREE, e),
  1336.                          TYPE_BINFO (type),
  1337.                          conversion ? LOOKUP_NO_CONVERSION : 0);
  1338.  
  1339.           if (rval != error_mark_node)
  1340.         {
  1341.           if (conversion)
  1342.             {
  1343.               error ("both constructor and type conversion operator apply");
  1344.               return error_mark_node;
  1345.             }
  1346.           /* call to constructor successful.  */
  1347.           rval = build_cplus_new (type, rval, 1);
  1348.           return rval;
  1349.         }
  1350.         }
  1351.       /* Type conversion successful/applies.  */
  1352.       if (conversion)
  1353.         {
  1354.           if (conversion == error_mark_node)
  1355.         error ("ambiguous pointer conversion");
  1356.           return conversion;
  1357.         }
  1358.  
  1359.       /* now try normal C++ assignment semantics.  */
  1360.       binfo = TYPE_BINFO (dtype);
  1361.       if (BINFO_TYPE (binfo) == type
  1362.           || (binfo = get_binfo (type, dtype, 1)))
  1363.         {
  1364.           if (binfo == error_mark_node)
  1365.         return error_mark_node;
  1366.         }
  1367.       if (binfo != NULL_TREE)
  1368.         {
  1369.           if (lvalue_p (e))
  1370.         {
  1371.           e = build_unary_op (ADDR_EXPR, e, 0);
  1372.  
  1373.           if (! BINFO_OFFSET_ZEROP (binfo))
  1374.             e = build (PLUS_EXPR, TYPE_POINTER_TO (type),
  1375.                    e, BINFO_OFFSET (binfo));
  1376.           return build1 (INDIRECT_REF, type, e);
  1377.         }
  1378.  
  1379.           sorry ("addressable aggregates");
  1380.           return error_mark_node;
  1381.         }
  1382.       error ("conversion between incompatible aggregate types requested");
  1383.       return error_mark_node;
  1384.     }
  1385.       /* conversion from non-aggregate to aggregate type requires
  1386.          constructor.  */
  1387.       else if (TYPE_HAS_CONSTRUCTOR (type))
  1388.     {
  1389.       tree rval;
  1390.       tree init = build_method_call (NULL_TREE, constructor_name_full (type),
  1391.                      build_tree_list (NULL_TREE, e),
  1392.                      TYPE_BINFO (type), LOOKUP_NORMAL);
  1393.       if (init == error_mark_node)
  1394.         {
  1395.           cp_error ("in conversion to type `%T'", type);
  1396.           return error_mark_node;
  1397.         }
  1398.       /* We can't pass 1 to the with_cleanup_p arg here, because that
  1399.              screws up passing classes by value.  */
  1400.       rval = build_cplus_new (type, init, 0);
  1401.       return rval;
  1402.     }
  1403.     }
  1404.  
  1405.   /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
  1406.      then the it won't be hashed and hence compare as not equal,
  1407.      even when it is.  */
  1408.   if (code == ARRAY_TYPE
  1409.       && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
  1410.       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
  1411.     return e;
  1412.  
  1413.   cp_error ("conversion from `%T' to non-scalar type `%T' requested",
  1414.         TREE_TYPE (expr), type);
  1415.   return error_mark_node;
  1416. }
  1417.  
  1418. /* Create an expression whose value is that of EXPR,
  1419.    converted to type TYPE.  The TREE_TYPE of the value
  1420.    is always TYPE.  This function implements all reasonable
  1421.    conversions; callers should filter out those that are
  1422.    not permitted by the language being compiled.  */
  1423.  
  1424. tree
  1425. convert (type, expr)
  1426.      tree type, expr;
  1427. {
  1428.   return cp_convert (type, expr, CONV_OLD_CONVERT, 0);
  1429. }
  1430.  
  1431. /* Like convert, except permit conversions to take place which
  1432.    are not normally allowed due to access restrictions
  1433.    (such as conversion from sub-type to private super-type).  */
  1434. tree
  1435. convert_force (type, expr)
  1436.      tree type;
  1437.      tree expr;
  1438. {
  1439.   register tree e = expr;
  1440.   register enum tree_code code = TREE_CODE (type);
  1441.  
  1442.   if (code == REFERENCE_TYPE)
  1443.     return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
  1444.                        NULL_TREE));
  1445.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1446.     e = convert_from_reference (e);
  1447.  
  1448.   if (code == POINTER_TYPE)
  1449.     return fold (convert_to_pointer_force (type, e));
  1450.  
  1451.   /* From typeck.c convert_for_assignment */
  1452.   if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
  1453.     && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
  1454.     && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
  1455.        || integer_zerop (e)
  1456.        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
  1457.       && TYPE_PTRMEMFUNC_P (type))
  1458.     {
  1459.       /* compatible pointer to member functions. */
  1460.       return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
  1461.     }
  1462.   {
  1463.     int old_equiv = flag_int_enum_equivalence;
  1464.     flag_int_enum_equivalence = 1;
  1465.     e = convert (type, e);
  1466.     flag_int_enum_equivalence = old_equiv;
  1467.   }
  1468.   return e;
  1469. }
  1470.  
  1471. /* Subroutine of build_type_conversion.  */
  1472. static tree
  1473. build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
  1474.      tree xtype, basetype;
  1475.      tree expr;
  1476.      tree typename;
  1477.      int for_sure;
  1478. {
  1479.   tree rval;
  1480.   int flags;
  1481.  
  1482.   if (for_sure == 0)
  1483.     flags = LOOKUP_PROTECT;
  1484.   else
  1485.     flags = LOOKUP_NORMAL;
  1486.  
  1487.   rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
  1488.   if (rval == error_mark_node)
  1489.     {
  1490.       if (for_sure == 0)
  1491.     return NULL_TREE;
  1492.       return error_mark_node;
  1493.     }
  1494.   if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
  1495.       && TREE_CODE (xtype) != REFERENCE_TYPE)
  1496.     rval = default_conversion (rval);
  1497.  
  1498.   if (warn_cast_qual
  1499.       && TREE_TYPE (xtype)
  1500.       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
  1501.       > TREE_READONLY (TREE_TYPE (xtype))))
  1502.     warning ("user-defined conversion casting away `const'");
  1503.   return convert (xtype, rval);
  1504. }
  1505.  
  1506. /* Convert an aggregate EXPR to type XTYPE.  If a conversion
  1507.    exists, return the attempted conversion.  This may
  1508.    return ERROR_MARK_NODE if the conversion is not
  1509.    allowed (references private members, etc).
  1510.    If no conversion exists, NULL_TREE is returned.
  1511.  
  1512.    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
  1513.    to take place immediately.  Otherwise, we build a SAVE_EXPR
  1514.    which can be evaluated if the results are ever needed.
  1515.  
  1516.    If FOR_SURE >= 2, then we only look for exact conversions.
  1517.  
  1518.    TYPE may be a reference type, in which case we first look
  1519.    for something that will convert to a reference type.  If
  1520.    that fails, we will try to look for something of the
  1521.    reference's target type, and then return a reference to that.  */
  1522. tree
  1523. build_type_conversion (code, xtype, expr, for_sure)
  1524.      enum tree_code code;
  1525.      tree xtype, expr;
  1526.      int for_sure;
  1527. {
  1528.   /* C++: check to see if we can convert this aggregate type
  1529.      into the required scalar type.  */
  1530.   tree type, type_default;
  1531.   tree typename = build_typename_overload (xtype), *typenames;
  1532.   int n_variants = 0;
  1533.   tree basetype, save_basetype;
  1534.   tree rval;
  1535.   int exact_conversion = for_sure >= 2;
  1536.   for_sure &= 1;
  1537.  
  1538.   if (expr == error_mark_node)
  1539.     return error_mark_node;
  1540.  
  1541.   basetype = TREE_TYPE (expr);
  1542.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1543.     basetype = TREE_TYPE (basetype);
  1544.  
  1545.   if (TYPE_PTRMEMFUNC_P (basetype) && TREE_CODE (xtype) == BOOLEAN_TYPE)
  1546.     {
  1547.       /* We convert a pointer to member function into a boolean,
  1548.      by just checking the index value, for == 0, we want false, for
  1549.      != 0, we want true.  */
  1550.       return convert (xtype, build_component_ref (expr, index_identifier, 0, 0));
  1551.     }
  1552.  
  1553.   basetype = TYPE_MAIN_VARIANT (basetype);
  1554.   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
  1555.     return NULL_TREE;
  1556.  
  1557.   if (TREE_CODE (xtype) == POINTER_TYPE
  1558.       || TREE_CODE (xtype) == REFERENCE_TYPE)
  1559.     {
  1560.       /* Prepare to match a variant of this type.  */
  1561.       type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1562.       for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
  1563.     n_variants++;
  1564.       typenames = (tree *)alloca (n_variants * sizeof (tree));
  1565.       for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1566.        type; n_variants++, type = TYPE_NEXT_VARIANT (type))
  1567.     {
  1568.       if (type == TREE_TYPE (xtype))
  1569.         typenames[n_variants] = typename;
  1570.       else if (TREE_CODE (xtype) == POINTER_TYPE)
  1571.         typenames[n_variants] = build_typename_overload (build_pointer_type (type));
  1572.       else
  1573.         typenames[n_variants] = build_typename_overload (build_reference_type (type));
  1574.     }
  1575.     }
  1576.  
  1577.   save_basetype = basetype;
  1578.   type = xtype;
  1579.  
  1580.   while (TYPE_HAS_CONVERSION (basetype))
  1581.     {
  1582.       int i;
  1583.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1584.     return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1585.       for (i = 0; i < n_variants; i++)
  1586.     if (typenames[i] != typename
  1587.         && lookup_fnfields (TYPE_BINFO (basetype), typenames[i], 0))
  1588.       return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
  1589.  
  1590.       if (TYPE_BINFO_BASETYPES (basetype))
  1591.     basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1592.       else
  1593.     break;
  1594.     }
  1595.  
  1596.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1597.     {
  1598. #if 0
  1599.       /* Only reference variable initializations can use a temporary; this
  1600.          must be handled elsewhere (like convert_to_reference and
  1601.          compute_conversion_costs).  */
  1602.  
  1603.       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  1604.       typename = build_typename_overload (type);
  1605.       basetype = save_basetype;
  1606.  
  1607.       /* May need to build a temporary for this.  */
  1608.       while (TYPE_HAS_CONVERSION (basetype))
  1609.     {
  1610.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1611.         {
  1612.           int flags;
  1613.  
  1614.           if (for_sure == 0)
  1615.         flags = LOOKUP_PROTECT;
  1616.           else
  1617.         flags = LOOKUP_NORMAL;
  1618.           rval = build_method_call (expr,
  1619.                     constructor_name_full (typename),
  1620.                     NULL_TREE, NULL_TREE, flags);
  1621.           if (rval == error_mark_node)
  1622.         {
  1623.           if (for_sure == 0)
  1624.             return NULL_TREE;
  1625.           return error_mark_node;
  1626.         }
  1627.  
  1628.           return convert (xtype, rval);
  1629.         }
  1630.       if (TYPE_BINFO_BASETYPES (basetype))
  1631.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1632.       else
  1633.         break;
  1634.     }
  1635. #endif
  1636.       /* No free conversions for reference types, right?.  */
  1637.       return NULL_TREE;
  1638.     }
  1639.  
  1640.   if (exact_conversion)
  1641.     return NULL_TREE;
  1642.  
  1643.   if (TREE_CODE (type) == BOOLEAN_TYPE)
  1644.     {
  1645.       tree as_int = build_type_conversion (code, long_long_unsigned_type_node, expr, 0);
  1646.       tree as_ptr = build_type_conversion (code, ptr_type_node, expr, 0);
  1647.       /* We are missing the conversion to pointer to member type. */
  1648.       /* We are missing the conversion to floating type. */
  1649.       if (as_int && as_ptr && for_sure)
  1650.     {
  1651.       cp_error ("ambiguous conversion from `%T' to `bool', can convert to integral type or pointer", TREE_TYPE (expr));
  1652.       return error_mark_node;
  1653.     }
  1654.       if (as_int)
  1655.     {
  1656.       as_int = build_type_conversion (code, long_long_unsigned_type_node, expr, for_sure+exact_conversion*2);
  1657.       return convert (xtype, as_int);
  1658.     }
  1659.       if (as_ptr)
  1660.     {
  1661.       as_ptr = build_type_conversion (code, ptr_type_node, expr, for_sure+exact_conversion*2);
  1662.       return convert (xtype, as_ptr);
  1663.     }
  1664.       return NULL_TREE;
  1665.     }
  1666.  
  1667.   /* No perfect match found, try default.  */
  1668. #if 0 /* This is wrong; there is no standard conversion from void* to
  1669.          anything.  -jason */
  1670.   if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
  1671.     type_default = ptr_type_node;
  1672.   else
  1673. #endif
  1674.   if (type == void_type_node)
  1675.     return NULL_TREE;
  1676.   else
  1677.     {
  1678.       tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
  1679.       if (tmp == error_mark_node)
  1680.     return NULL_TREE;
  1681.       type_default = TREE_TYPE (tmp);
  1682.     }
  1683.  
  1684.   basetype = save_basetype;
  1685.  
  1686.   if (type_default != type)
  1687.     {
  1688.       type = type_default;
  1689.       typename = build_typename_overload (type);
  1690.  
  1691.       while (TYPE_HAS_CONVERSION (basetype))
  1692.     {
  1693.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1694.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1695.       if (TYPE_BINFO_BASETYPES (basetype))
  1696.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1697.       else
  1698.         break;
  1699.     }
  1700.     }
  1701.  
  1702.   if (TREE_CODE (type) == POINTER_TYPE && TYPE_READONLY (TREE_TYPE (type)))
  1703.     {
  1704.       /* Try converting to some other const pointer type and then using
  1705.          standard conversions. */
  1706.  
  1707.       while (TYPE_HAS_CONVERSION (basetype))
  1708.     {
  1709.       if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
  1710.         {
  1711.           if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
  1712.         return error_mark_node;
  1713.           typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
  1714.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1715.         }
  1716.       if (TYPE_BINFO_BASETYPES (basetype))
  1717.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1718.       else
  1719.         break;
  1720.     }
  1721.     }
  1722.   if (TREE_CODE (type) == POINTER_TYPE)
  1723.     {
  1724.       /* Try converting to some other pointer type and then using standard
  1725.      conversions.  */
  1726.  
  1727.       while (TYPE_HAS_CONVERSION (basetype))
  1728.     {
  1729.       if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
  1730.         {
  1731.           if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
  1732.         return error_mark_node;
  1733.           typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
  1734.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1735.         }
  1736.       if (TYPE_BINFO_BASETYPES (basetype))
  1737.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1738.       else
  1739.         break;
  1740.     }
  1741.     }
  1742.  
  1743.   /* Use the longer or shorter conversion that is appropriate.  Have
  1744.      to check against 0 because the conversion may come from a baseclass.  */
  1745.   if (TREE_CODE (type) == INTEGER_TYPE
  1746.       && TYPE_HAS_INT_CONVERSION (basetype)
  1747.       && CLASSTYPE_CONVERSION (basetype, int_conv) != 0
  1748.       && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
  1749.     {
  1750.       typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
  1751.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1752.     }
  1753.  
  1754.   if (TREE_CODE (type) == REAL_TYPE
  1755.       && TYPE_HAS_REAL_CONVERSION (basetype)
  1756.       && CLASSTYPE_CONVERSION (basetype, real_conv) != 0
  1757.       && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
  1758.     {
  1759.       /* Only accept using an operator double() if there isn't a conflicting
  1760.      operator int().  */
  1761.       if (TYPE_HAS_INT_CONVERSION (basetype))
  1762.     {
  1763.       if (for_sure)
  1764.         {
  1765.           cp_error ("two possible conversions for type `%T'", type);
  1766.           return error_mark_node;
  1767.         }
  1768.       else
  1769.         return NULL_TREE;
  1770.     }
  1771.  
  1772.       typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
  1773.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1774.     }
  1775.  
  1776.   /* THESE ARE TOTAL KLUDGES.  */
  1777.   /* Default promotion yields no new alternatives, try
  1778.      conversions which are anti-default, such as
  1779.  
  1780.      double -> float or int -> unsigned or unsigned -> long
  1781.  
  1782.      */
  1783.   if (type_default == type
  1784.       && (INTEGRAL_TYPE_P (type) || TREE_CODE (type) == REAL_TYPE))
  1785.     {
  1786.       int not_again = 0;
  1787.  
  1788.       if (type == double_type_node)
  1789.     typename = build_typename_overload (float_type_node);
  1790.       else if (type == integer_type_node)
  1791.     typename = build_typename_overload (unsigned_type_node);
  1792.       else if (type == unsigned_type_node)
  1793.     typename = build_typename_overload (long_integer_type_node);
  1794.  
  1795.     again:
  1796.       basetype = save_basetype;
  1797.       while (TYPE_HAS_CONVERSION (basetype))
  1798.     {
  1799.       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1800.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1801.       if (TYPE_BINFO_BASETYPES (basetype))
  1802.         basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1803.       else
  1804.         break;
  1805.     }
  1806.       if (! not_again)
  1807.     {
  1808.       if (type == integer_type_node)
  1809.         {
  1810.           typename = build_typename_overload (long_integer_type_node);
  1811.           not_again = 1;
  1812.           goto again;
  1813.         }
  1814.       else
  1815.         {
  1816.           typename = build_typename_overload (integer_type_node);
  1817.           not_again = 1;
  1818.           goto again;
  1819.         }
  1820.     }
  1821.     }
  1822.  
  1823.   /* Now, try C promotions...
  1824.  
  1825.      float -> int
  1826.      int -> float  */
  1827.  
  1828.     basetype = save_basetype;
  1829.     if (TREE_CODE (type) == REAL_TYPE)
  1830.       type = integer_type_node;
  1831.     else if (TREE_CODE (type) == INTEGER_TYPE)
  1832.       if (TYPE_HAS_REAL_CONVERSION (basetype))
  1833.     type = double_type_node;
  1834.       else
  1835.     return NULL_TREE;
  1836.     else
  1837.       return NULL_TREE;
  1838.  
  1839.     typename = build_typename_overload (type);
  1840.     while (TYPE_HAS_CONVERSION (basetype))
  1841.       {
  1842.     if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1843.       {
  1844.         rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1845.         return rval;
  1846.       }
  1847.     if (TYPE_BINFO_BASETYPES (basetype))
  1848.       basetype = TYPE_BINFO_BASETYPE (basetype, 0);
  1849.     else
  1850.       break;
  1851.       }
  1852.  
  1853.   return NULL_TREE;
  1854. }
  1855.  
  1856. /* Must convert two aggregate types to non-aggregate type.
  1857.    Attempts to find a non-ambiguous, "best" type conversion.
  1858.  
  1859.    Return 1 on success, 0 on failure.
  1860.  
  1861.    @@ What are the real semantics of this supposed to be??? */
  1862. int
  1863. build_default_binary_type_conversion (code, arg1, arg2)
  1864.      enum tree_code code;
  1865.      tree *arg1, *arg2;
  1866. {
  1867.   tree type1 = TREE_TYPE (*arg1);
  1868.   tree type2 = TREE_TYPE (*arg2);
  1869.  
  1870.   if (TREE_CODE (type1) == REFERENCE_TYPE
  1871.       || TREE_CODE (type1) == POINTER_TYPE)
  1872.     type1 = TREE_TYPE (type1);
  1873.   if (TREE_CODE (type2) == REFERENCE_TYPE
  1874.       || TREE_CODE (type2) == POINTER_TYPE)
  1875.     type2 = TREE_TYPE (type2);
  1876.  
  1877.   if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
  1878.     {
  1879.       tree decl = typedecl_for_tag (type1);
  1880.       if (decl)
  1881.     error ("type conversion nonexistent for type `%s'",
  1882.            IDENTIFIER_POINTER (DECL_NAME (decl)));
  1883.       else
  1884.     error ("type conversion nonexistent for non-C++ type");
  1885.       return 0;
  1886.     }
  1887.   if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
  1888.     {
  1889.       tree decl = typedecl_for_tag (type2);
  1890.       if (decl)
  1891.     error ("type conversion nonexistent for type `%s'",
  1892.            IDENTIFIER_POINTER (decl));
  1893.       else
  1894.     error ("type conversion nonexistent for non-C++ type");
  1895.       return 0;
  1896.     }
  1897.  
  1898.   if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
  1899.     {
  1900.       if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
  1901.     cp_error ("no conversion from `%T' and `%T' to types with default `%O' ",
  1902.           type1, type2, code);
  1903.       else
  1904.     cp_error ("no conversion from `%T' to type with default `%O'",
  1905.           type1, code);
  1906.       return 0;
  1907.     }
  1908.   else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
  1909.     {
  1910.       cp_error ("no conversion from `%T' to type with default `%O'",
  1911.         type2, code);
  1912.       return 0;
  1913.     }
  1914.  
  1915.   if (code == TRUTH_ANDIF_EXPR
  1916.       || code == TRUTH_ORIF_EXPR)
  1917.     {
  1918.       *arg1 = convert (bool_type_node, *arg1);
  1919.       *arg2 = convert (bool_type_node, *arg2);
  1920.     }
  1921.   else if (TYPE_HAS_INT_CONVERSION (type1))
  1922.     {
  1923.       if (TYPE_HAS_REAL_CONVERSION (type1))
  1924.     cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int",
  1925.             type1);
  1926.       *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
  1927.       *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
  1928.     }
  1929.   else if (TYPE_HAS_REAL_CONVERSION (type1))
  1930.     {
  1931.       *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
  1932.       *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
  1933.     }
  1934.   else
  1935.     {
  1936.       *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
  1937.       if (*arg1 == error_mark_node)
  1938.     error ("ambiguous pointer conversion");
  1939.       *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
  1940.       if (*arg1 != error_mark_node && *arg2 == error_mark_node)
  1941.     error ("ambiguous pointer conversion");
  1942.     }
  1943.   if (*arg1 == 0)
  1944.     {
  1945.       if (*arg2 == 0 && type1 != type2)
  1946.     cp_error ("default type conversion for types `%T' and `%T' failed",
  1947.           type1, type2);
  1948.       else
  1949.     cp_error ("default type conversion for type `%T' failed", type1);
  1950.       return 0;
  1951.     }
  1952.   else if (*arg2 == 0)
  1953.     {
  1954.       cp_error ("default type conversion for type `%T' failed", type2);
  1955.       return 0;
  1956.     }
  1957.   return 1;
  1958. }
  1959.  
  1960. /* Must convert an aggregate type to non-aggregate type.
  1961.    Attempts to find a non-ambiguous, "best" type conversion.
  1962.  
  1963.    Return 1 on success, 0 on failure.
  1964.  
  1965.    The type of the argument is expected to be of aggregate type here.
  1966.  
  1967.    @@ What are the real semantics of this supposed to be??? */
  1968. int
  1969. build_default_unary_type_conversion (code, arg)
  1970.      enum tree_code code;
  1971.      tree *arg;
  1972. {
  1973.   tree type = TREE_TYPE (*arg);
  1974.  
  1975.   if (! TYPE_HAS_CONVERSION (type))
  1976.     {
  1977.       cp_error ("type conversion required for type `%T'", type);
  1978.       return 0;
  1979.     }
  1980.  
  1981.   if (code == TRUTH_NOT_EXPR)
  1982.     *arg = convert (bool_type_node, *arg);
  1983.   else if (TYPE_HAS_INT_CONVERSION (type))
  1984.     {
  1985.       if (TYPE_HAS_REAL_CONVERSION (type))
  1986.     cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int",
  1987.             type);
  1988.       *arg = build_type_conversion (code, integer_type_node, *arg, 1);
  1989.     }
  1990.   else if (TYPE_HAS_REAL_CONVERSION (type))
  1991.     *arg = build_type_conversion (code, double_type_node, *arg, 1);
  1992.   else
  1993.     {
  1994.       *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
  1995.       if (*arg == error_mark_node)
  1996.     error ("ambiguous pointer conversion");
  1997.     }
  1998.   if (*arg == NULL_TREE)
  1999.     {
  2000.       cp_error ("default type conversion for type `%T' failed", type);
  2001.       return 0;
  2002.     }
  2003.   return 1;
  2004. }
  2005.  
  2006. /* Implements integral promotion (4.1) and float->double promotion. */
  2007. tree
  2008. type_promotes_to (type)
  2009.      tree type;
  2010. {
  2011.   int constp = TYPE_READONLY (type);
  2012.   int volatilep = TYPE_VOLATILE (type);
  2013.   type = TYPE_MAIN_VARIANT (type);
  2014.  
  2015.   /* bool always promotes to int (not unsigned), even if it's the same
  2016.      size.  */
  2017.   if (type == bool_type_node)
  2018.     type = integer_type_node;
  2019.  
  2020.   /* Normally convert enums to int, but convert wide enums to something
  2021.      wider.  */
  2022.   else if (TREE_CODE (type) == ENUMERAL_TYPE
  2023.        || type == wchar_type_node)
  2024.     type = type_for_size
  2025.       (MAX (TYPE_PRECISION (type), TYPE_PRECISION (integer_type_node)),
  2026.        (flag_traditional
  2027.     || (TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node)))
  2028.        && TREE_UNSIGNED (type));
  2029.   else if (C_PROMOTING_INTEGER_TYPE_P (type))
  2030.     {
  2031.       /* Traditionally, unsignedness is preserved in default promotions.
  2032.          Otherwise, retain unsignedness if really not getting bigger.  */
  2033.       if (TREE_UNSIGNED (type)
  2034.       && (flag_traditional
  2035.           || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
  2036.     type = unsigned_type_node;
  2037.       else
  2038.     type = integer_type_node;
  2039.     }
  2040.   else if (type == float_type_node)
  2041.     type = double_type_node;
  2042.  
  2043.   return c_build_type_variant (type, constp, volatilep);
  2044. }
  2045.